package com.food.restaurantpro.fragments;

import java.util.Observable;
import java.util.Observer;

import com.food.restaurantpro.R;
import com.food.restaurantpro.activity.MainActivity;
import com.food.restaurantpro.connection.DataHelper;
import com.food.restaurantpro.connection.DataResult;
import com.food.restaurantpro.connection.IServerService.Methods;
import com.food.restaurantpro.imageloader.ImageLoader;
import com.food.restaurantpro.managingnetwork.ManagingNetwork;
import com.food.restaurantpro.objects.AboutUs;
import com.food.restaurantpro.utils.Utils;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class AboutUsFragment extends Fragment {
	private TextView tvHeader;
	private TextView tvAboutUsInfo;
	private ImageView ivCover;
	private ImageLoader mImageLoader;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mImageLoader = new ImageLoader(getActivity(), R.drawable.aboutus, 0);
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View view = inflater.inflate(R.layout.fragment_aboutus, container, false);
		tvHeader = ((TextView)view.findViewById(R.id.tvHeader));
		tvHeader.setText(R.string.about_us);
		tvAboutUsInfo = ((TextView)view.findViewById(R.id.tvAboutUsInfo));
		ivCover = ((ImageView)view.findViewById(R.id.imgvAboutUs));
		AboutUs aboutUs = AboutUs.getAboutUs();
		updateAboutUsUI(aboutUs);
		if(ManagingNetwork.isNetworkAvailable()) {
			callApiGetAboutUs();
		}
		return view;
	}
	
	private void updateAboutUsUI(AboutUs aboutUs) {
		if(!Utils.isEmpty(aboutUs.getTitle())) {
			tvHeader.setText(aboutUs.getTitle());
		}
		if(!Utils.isEmpty(aboutUs.getContent())) {
			tvAboutUsInfo.setText(aboutUs.getContent());
		}
		if(!Utils.isEmpty(aboutUs.getImageURL())) {
			mImageLoader.displayImage(aboutUs.getImageURL(), ivCover);
		}
	}
	
	private void callApiGetAboutUs() {
		DataHelper.getInstance().addObserver(new Observer() {
			@Override
			public void update(Observable observable, Object data) {
				DataResult result = (DataResult) data;
				if (result.getMethodName().equals(Methods.getAboutUs)) {
//					((MainActivity)getActivity()).hideProcessDialog();
					AboutUs about = (AboutUs) result.getData();
					if(about != null) {
						updateAboutUsUI(about);
						//luu xuong database
						AboutUs.saveAboutUs(about);
					}
					DataHelper.getInstance().deleteObserver(this);
				}
			}
		});
//		((MainActivity)getActivity()).showProcessDialog();
		DataHelper.getInstance().getAboutUs();
	}
	
	@Override
	public void onDestroy() {
		// TODO delete image and cache de giai phong bo nho
		super.onDestroy();
		if(mImageLoader != null) {
			mImageLoader.clearMemoryCache();
		}
		Runtime.getRuntime().gc();
	}
}
